package com.test.controller;

import com.test.pojo.Food;
import com.test.pojo.FoodType;
import com.test.service.FoodService;
import com.test.service.FoodTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class FoodCtrl {

    @Resource
    private FoodService foodService;
    @Resource
    private FoodTypeService foodTypeService;

    /*
     *获取所有的菜品
     */
    @RequestMapping("/getAllFoodList")
    public String getAllFoodList(Model model){
        List<Food> foodList = foodService.getAllFoodList();
        model.addAttribute("foodList",foodList);
        return "FoodList";
    }

    /*int
     *跳转到菜品更新页面
     */
    @GetMapping(value = "/editFoodList")
    public String searchFoodTypeByID(@RequestParam("foodId") int id, Model model){
        List<FoodType> foodTypeList = foodTypeService.getAllFoodType();
        model.addAttribute("foodTypeList",foodTypeList);

        Food food = foodService.getFoodByID(id);
        if (food != null) {
            model.addAttribute("food",food);
            return "EditFoodList";
        }else {
            return "redirect:/getAllFoodList";
        }
    }

    /*
     * 更改菜系名称
     * */
    @PostMapping(value ="/doEditFoodList/{id}")
    public String doEditFoodType(@PathVariable("id") int id, Food food, Model model){



        int num = foodService.updateFood(food);
        if(num > 0){
            model.addAttribute("msg","菜品更新成功！");
        }else{
            model.addAttribute("msg","菜品更新失败！");
        }
        return "forward:/getAllFoodList";
    }


    /*
     * 转到添加页面
     * */
    @GetMapping(value = "/goToAddFootList")
    public  String goToAddFootList(Model model){
        List<FoodType> foodTypeList = foodTypeService.getAllFoodType();
        model.addAttribute("foodTypeList",foodTypeList);
        return "AddFoodList";
    }



    /*
     * 添加菜品
     **/
    @PostMapping(value ="/doAddFootList")
    public String addFootList(Food food, Model model){

        int num = foodService.addFoodList(food);
        if(num > 0){
            model.addAttribute("msg","菜品【"+food.getFoodName()+"】添加成功！");
        }else{
            model.addAttribute("msg","菜品添加失败！");
        }
        return "forward:/getAllFoodList";
    }


    /*删除菜品*/
    @RequestMapping(value ="/deleteFoodList/{id}",method = RequestMethod.GET)
    public String delFoodType(@PathVariable("id") int id, Model model){
        int num = foodService.deleteFoodList(id);
        System.out.println(num);
        if(num > 0){
            model.addAttribute("msg","菜品删除成功！");
        }else if (num ==0){
            model.addAttribute("msg","菜品删除失败！");
        }
        return "forward:/getAllFoodList";

    }
}
